do_top_10_wards_intensity_desc<- function( con,
          dt_hosp_raw = get_dt_hosp(top_All_prof_10 = top_All_prof_10) ,
          data = new_test_cnr) { 
  
  # missing actual_cost is zero (we can check the effect ion average )

    top_10_cost_per_day <- rbind(
    dt_hosp_raw[ n_rows ==1  ,
                 .(.N,
                   sum_days= sum(amount),
                   cost_day = sum(actual_cost) / sum(amount) ),
                 by = .(new_profession)][
                   N>10][,group:="All"]
    ,dt_hosp_raw[ n_rows ==1  ,
                  .(.N,
                    sum_days=   sum(amount),
                    cost_day = sum(actual_cost) / sum(amount) ),
                  by = .(new_profession, 
                         group = factor(ifelse(DMG_died_within_365d=="1", 
                                               "Decedent",
                                               "Survivor")))][
                                                 N>10]
  )[order(group, cost_day, decreasing = T)]
  

    dt_surgery_adm10 <- data.table::as.data.table(
    DBI::dbGetQuery(
      con,
      paste0("sql_query"
      )
    )
  )
  
  # Only obs od test set : 
  dt_surgery_adm_res10 <- 
    rbind (dt_surgery_adm10[id_var%in%
                              data[,id_var],
                            .(.N,
                              surgery_sh=mean(Surgery ) ),
                            by = .( ward_top10  , died_within_year)][
                              order( ward_top10  , died_within_year)
                              ],
           dt_surgery_adm10[id_var%in%
                              data[,id_var],
                            .(.N,
                              surgery_sh=mean(Surgery ) ),
                            by = .( ward_top10  )][,died_within_year := "All"
                                                   ][  order( ward_top10  ) ]
    )

  table_adm_per_day_pub_death_with_surgery10 <-   merge(
    x = top_10_cost_per_day,
    y = dt_surgery_adm_res10[,
                             .(
                               group         = factor(case_when(died_within_year=="0" ~ "Survivor",
                                                                died_within_year=="1" ~ "Decedent",
                                                                died_within_year=="All" ~ "All")),
                               new_profession    = factor(ifelse(ward_top10=="Others",
                                                                 "Other",
                                                                 as.character(ward_top10))),
                               surgery_sh   = round(surgery_sh,3) ) ],
    by= c("group" , "new_profession" ),
    all.x = T, 
    all.y = F 
  )[order(new_profession ,group )][,new_profession := gsub("_"," ",new_profession)]
  
  
  
  table_adm_per_day_pub_death_with_surgery10[ , share_hosp := N/sum(N), by = group]
  table_adm_per_day_pub_death_with_surgery10[ , share_days:= sum_days /sum(sum_days  ), by = group]
  
  
  
  table_adm_per_day_pub_death_with_surgery10_pub <- 
    table_adm_per_day_pub_death_with_surgery10[order(group,-cost_day)][,
                                                                       .( group  ,
                                                                          new_profession= Hmisc::capitalize(as.character(new_profession)),
                                                                          cost_day=scales::comma(cost_day),
                                                                          surgery_sh= round(surgery_sh*100,1),
                                                                          share_hosp =round(share_hosp*100,1)  ,
                                                                          share_days = round(share_days*100,1)
                                                                       )]
  
  table_adm_per_day_pub_death_with_surgery10_pub[, Intensity := rep(c("High",rep("",6),"Low",rep("",3)),3)]
  
  
  setcolorder(table_adm_per_day_pub_death_with_surgery10_pub,"Intensity")
  
  
  
  table_adm_per_day_pub_death_with_surgery10_pub[,new_profession := tools::toTitleCase(new_profession)]
  
  
  invisible(Hmisc::latex(
    table_adm_per_day_pub_death_with_surgery10_pub[group == "All",-2],
    file = paste0("top_10_wards_intensity_desc.tex"),
    center = 'centering',
    colheads = c("Intensity",
                 "Ward",
                 "\\thead{Average\\\\Daily\\\\Cost (NIS)}",
                 "\\thead{Share With\\\\Surgical\\\\Procedure}",
                 "\\thead{Share of\\\\Admission}",
                 "\\thead{Share of\\\\Days}"),
    rowname =NULL,
    n.rgroup = c(7, 4),
    rowlabel = "",
    col.just = c("l","l", rep.int("r", 4)),
    extracolheads = c("","","(1)", "(2)", "(3)", "(4)"), 
    extracolsize = "normalsize"
  ))

  invisible(Hmisc::latex(
    table_adm_per_day_pub_death_with_surgery10_pub[group != "All", -c(1,2)],
    file = paste0("top_10_wards_intensity_desc_DS.tex"),
    center = 'centering',
    colheads = c("Ward",
                 "\\thead{Average\\\\Daily\\\\Cost (NIS)}",
                 "\\thead{Share With\\\\Surgical\\\\Procedure}",
                 "\\thead{Share of\\\\Admission}",
                 "\\thead{Share of\\\\Days}"),
    n.rgroup = c(7,4,7,4),
    rgroup = c("A. Decedent~~~","" ,"B. Survivor~~~",""),
    rowname = rep(c("~~~High",rep("",6),"~~~Low",rep("",3)),2),
    rowlabel = "Intensity",
    col.just = c("l", rep.int("r", 4)),
    extracolheads = c("","(1)", "(2)", "(3)", "(4)"),
    extracolsize = "normalsize"
  ))
  
  write.csv(table_adm_per_day_pub_death_with_surgery10, file = "Cost_per_admiss_day_top10.csv")
  
  return( table_adm_per_day_pub_death_with_surgery10_pub)
  
}
